package helipay.api.web.controller.webservice;

import helipay.api.model.Orders;
import helipay.api.model.OrdersSwipeCard;
import helipay.api.service.AppPayService;
import helipay.api.util.DateUtil;
import helipay.api.util.I18nUtil;
import helipay.api.util.ResultUtil;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by yimi on 2017/4/13.
 */
@Controller
public class OrderController {

    @Autowired
    private AppPayService appPayService;

    @RequestMapping(value = "/addOrderInfo")
    public String addOrderInfo(ModelMap model, String P1_bizType, String P2_orderId, String P3_customerNumber, String P4_payType, String P5_orderAmount, String P6_currency, String P7_authcode, String P8_appType,
                               String P11_orderIp, String P12_goodsName, String P13_goodsDetail, String P14_desc, String status, String invoiceStatus, String serialNumber, String date) {

        Assert.hasText(P1_bizType, "P1_bizType" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P2_orderId, "P2_orderId" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P3_customerNumber, "P3_customerNumber" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P4_payType, "P4_payType" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P5_orderAmount, "P5_orderAmount" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P6_currency, "P6_currency" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P7_authcode, "P7_authcode" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P8_appType, "P8_appType" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P11_orderIp, "P11_orderIp" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(P12_goodsName, "P12_goodsName" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(status, "status" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(invoiceStatus, "invoiceStatus" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(serialNumber, "serialNumber" + I18nUtil.getMessage(1012, null, "zh_CN"));

        Orders orders = addOrdersInfo(P1_bizType, P2_orderId, P3_customerNumber, P4_payType, P5_orderAmount, P6_currency, P7_authcode, P8_appType,
                P11_orderIp, P12_goodsName, P13_goodsDetail, P14_desc, status, invoiceStatus, serialNumber, date);

        model.put("message", appPayService.addOrderInfo(orders));


        return "message.json";
    }


    @RequestMapping(value = "/addSCOrderInfo")
    public String addSCOrderInfo(ModelMap model, String bizType, String customerNumber, String orderAmount, String authcode,
                                 String serialNumber, String date, String bankNo) {

        Assert.hasText(bizType, "bizType" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(customerNumber, "customerNumber" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(orderAmount, "orderAmount" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(authcode, "authcode" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(serialNumber, "serialNumber" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(date, "date" + I18nUtil.getMessage(1012, null, "zh_CN"));
        Assert.hasText(bankNo, "bankNo" + I18nUtil.getMessage(1012, null, "zh_CN"));

        OrdersSwipeCard orders = new OrdersSwipeCard();
        orders.setSerialNumber(serialNumber);
        orders.setCreatedTime(DateUtil.parseDate(DateUtil.TIME_PATTERN_DEFAULT, date));
        orders.setCreatedBy("admin");
        orders.setAuthCode(authcode);
        orders.setBizType(bizType);
        orders.setCustomerNumber(customerNumber);
        orders.setOrderAmount(NumberUtils.toDouble(orderAmount));
        orders.setStatus(0);
        orders.setBankNo(bankNo);
        model.put("message", appPayService.addSCOrderInfo(orders));


        return "message.json";
    }

    @RequestMapping(value = "/getOrderList")
    public String getOrderList(ModelMap model, String customerNumber, String appType, String startDate, String endDate, Integer index) {
        if (appType.equals("WXPAY") || appType.equals("ALIPAY") || appType.equals("SWIPE")) {
            model.put("message", ResultUtil.getResultJson(appPayService.getOrdersListByAppType(customerNumber, appType, startDate, endDate, index)));
        } else {
            throw new RuntimeException("异常");
        }

        return "message.json";
    }


    private Orders addOrdersInfo(String P1_bizType, String P2_orderId, String P3_customerNumber, String P4_payType, String P5_orderAmount, String P6_currency, String P7_authcode, String P8_appType,
                                 String P11_orderIp, String P12_goodsName, String P13_goodsDetail, String P14_desc, String status, String invoiceStatus, String serialNumber, String date) {
        Orders orders = new Orders();
        orders.setBizType(P1_bizType);
        orders.setOrderId(P2_orderId);
        orders.setCustomerNumber(P3_customerNumber);
        orders.setPayType(P4_payType);
        orders.setOrderAmount(NumberUtils.toDouble(P5_orderAmount));
        orders.setCurrency(P6_currency);
        orders.setAuthCode(P7_authcode);
        orders.setAppType(P8_appType);
        orders.setOrderIp(P11_orderIp);
        orders.setGoodsName(P12_goodsName);
        orders.setGoodsDetail(P13_goodsDetail);
        orders.setGoodsDesc(P14_desc);
        orders.setStatus(status);
        orders.setInvoiceStatus(NumberUtils.toInt(invoiceStatus));
        orders.setSerialNumber(serialNumber);
        orders.setCreatedTime(DateUtil.parseDate(DateUtil.TIME_PATTERN_DEFAULT, date));
        orders.setCreatedBy("admin");
        return orders;


    }
}
